#include "book.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include<iomanip>
using namespace std;

//构造函数
Book::Book(const char* name,int _m_id,int _a_id,int borrow,int num) 
{
	strcpy(m_name,name);
	m_id = _m_id;
	a_id = 0;
	m_borrow = borrow;
	m_cnt = 0;
	m_score = 0;
	m_num = 1;
	bzero(m_inTime,sizeof(m_inTime));   //借书时间显示
	bzero(m_outTime,sizeof(m_outTime));  //还书时间显示
	m_inTimeS = 0;     //借书时间秒存储
	m_outTimeS = 0;	  //还书时间秒存储
}


//借书
void Book::BorrowBook(void)
{
	bzero(m_inTime,sizeof(m_inTime));
	m_cnt++;
	m_borrow = 1;
	a_id = 0;
	m_inTimeS = time(NULL);
	SaveIntime();
}

//还书，返回还书与借书的相差的秒数
int Book::ReturnBook(int score)
{
	m_score =  ((m_cnt-1)*m_score + score)/m_cnt;
	m_borrow = 0;
	m_outTimeS = time(NULL);
	SaveOutime();
	cout << m_outTime << "    "  << m_inTime << endl;
	return (m_outTimeS-m_inTimeS);
}

void Book::SaveIntime(void)
{
	time_t timer = time(NULL);
	struct tm *t1;
	t1 = localtime(&timer);
	sprintf(m_inTime,"%d-%d-%d %d:%d:%d",t1->tm_year+1900,t1->tm_mon+1,t1->tm_mday,t1->tm_hour,t1->tm_min,t1->tm_sec);
}
void Book::SaveOutime()
{
	time_t timer = time(NULL);
	struct tm *t2;
	t2 = localtime(&timer);
	sprintf(m_outTime,"%d-%d-%d %d:%d:%d",t2->tm_year+1900,t2->tm_mon+1,t2->tm_mday,t2->tm_hour,t2->tm_min,t2->tm_sec);
}
//返回编号
int Book::GetId(void)
{
	return m_id;
}

//返回借阅人的数量
int Book::GetCnt(void)
{
	return m_cnt;
}
//返回评分
float Book::GetScore(void)
{
	return m_score;
}
//返回书的预约信息
int Book::GetAppoint(void)
{
	return a_id;
}

//返回书的在借
bool Book::GetBorrow(void)
{
	return m_borrow;
}

//返回书名
const char* Book::GetName(void)
{
	return m_name;
}
void Book::SetName(const char* name)
{
	strcpy(m_name,name);
}
void Book::SetAppoint(int id)
{
	a_id = id;
}
//显示书的信息
void Book::Show(void)
{
	cout << left << setw(20) << m_id << left << setw(20) << m_name << left << setw(20) << m_inTime << left << setw(20) << m_outTime << left << setw(20) << m_cnt << left << setw(20) << m_score << endl;
}

void Book::AllShow(void)
{
cout << left << setw(20) << m_id << left << setw(20) << m_name << left << setw(20) << m_borrow << left << setw(20) << a_id << left << setw(20) << m_cnt << left << setw(20) << m_score <<endl;
}

//析构
Book::~Book()
{
}
